package com.xgkj.diyiketang.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.AnchorListAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.AnchorListTask;
import com.xgkj.diyiketang.util.EmptyDataUtil;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

public class MyAttentionActivity extends BaseActivity {
    private Context mContext;
    private PullToRefreshGridView mListView;
    private AnchorListAdapter mAdapter;
    private int mMyAttentionTaskId = -1;
    private int size = 10;
    private int currentPage = 1;
    private LinearLayout mEmptyView;

    private LinearLayout mEmptyContentView;

    private String mUserId;
    private String  type;
    private TextView mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myattention);
        PalLog.e("aaaaaaa","oncreate myattention");
        mContext = this;
        initView();
    }

    private void getMyAttentionList(String userId,String type){
        if(currentPage == 0){
            mListView.onRefreshComplete();
            mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
//            ToastUtil.showMessage(mContext,R.string.no_more);
            return;
        }
        if(mMyAttentionTaskId != -1){
            return;
        }
        AnchorListTask myAttentionListTask = new AnchorListTask(size,currentPage,userId,"","",type,null);
        myAttentionListTask.addListener(myAttentionListener);
        mMyAttentionTaskId = getEatShowApp().getTaskManager().addTask(myAttentionListTask);
    }

    private TaskEventListener myAttentionListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == mMyAttentionTaskId){
                mMyAttentionTaskId = -1;
                mListView.onRefreshComplete();
                if (currentPage == 1){
                    mAdapter.clearData();
                }
                List<EatShowUserInfo> attentionList = new ArrayList<EatShowUserInfo>();
                if(data != null){
                    attentionList = (List<EatShowUserInfo>) data;
                }

                if(attentionList != null && attentionList.size()>0){
                    mAdapter.updateData(attentionList);
                }

                if(currentPage ==1 && mAdapter.getCount()>0){
                    mListView.setMode(PullToRefreshBase.Mode.BOTH);
                }

                currentPage = result;
                if(mAdapter != null && mAdapter.getCount() == 0){
                    EmptyDataUtil.updateEmptyContentViewGrid(mEmptyView, mEmptyContentView, mListView);
                }else{
                    EmptyDataUtil.updateNoEmptyContentViewGrid(mEmptyView, mEmptyContentView, mListView);
                }
            }

        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == mMyAttentionTaskId){
                mMyAttentionTaskId = -1;
                mListView.onRefreshComplete();
            }
        }
    };



    private void initView() {
        mTitle = (TextView) findViewById(R.id.title);
        mEmptyView = (LinearLayout) findViewById(R.id.content_error_empty);
        mEmptyView.setVisibility(View.GONE);
        mEmptyContentView = (LinearLayout) findViewById(R.id.empty_content_view);
        mEmptyContentView.setVisibility(View.GONE);
        mListView = (PullToRefreshGridView) findViewById(R.id.myattention_list);
       // mListView.setVisibility(View.VISIBLE);
        mAdapter = new AnchorListAdapter(mContext);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                EatShowUserInfo info = (EatShowUserInfo) mAdapter.getItem(position);
//                startActivity(AnchorActivity.getIntent(mContext,info.getUserId()));
                Intent intent = new Intent(mContext, HisDetailActivity.class);
                intent.putExtra("USERID", info.getUserId());

                startActivity(intent);
            }
        });
        mListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        ((TextView)findViewById(R.id.back)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mListView.setOnRefreshListener(new OnRefreshListener2<GridView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
                currentPage = 1;
                getMyAttentionList(mUserId,type);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
                getMyAttentionList(mUserId,type);
            }
        });

        Intent intent = getIntent();
        if (intent != null){
            mUserId = intent.getStringExtra("USERID");
        }

        if (mUserId != null){
            mTitle.setText("ta的关注");
            type = "2";
            getMyAttentionList(mUserId,type);
        }else {
            mTitle.setText("我的关注");
            type = "1";
            getMyAttentionList(null,type);
        }

        mListView.setRefreshing(true);

    }

    @Override
    protected void onResume() {
        super.onResume();
        currentPage = 1;
        getMyAttentionList(mUserId,type);
    }
}
